package game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Psycho
 * @create 2021-07-18-9:05
 */
public class MainFrame extends JFrame {
    private Node food;          //食物
    private Node food1;          //食物
    private JPanel jPanel;     //游戏棋盘
    private Snake snake;    //定义蛇的成员变量
    private Timer timer;       //
    //创建窗体对象时，初始化窗体参数
    public MainFrame(){
        //初始化窗体
        initFrame();
        //初始化游戏棋盘
        initGamePanel();
        //创建蛇
        initSnake();
        //初始化定时器，窗口重画
        initTimer();
        //定义一个键盘监听器
        setKeyListener();
    }

    private void setKeyListener() {
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                switch (e.getKeyCode()){
                    case KeyEvent.VK_UP:
                        if(snake.getDirection()!=Direction.DOWN){
                            snake.setDirection(Direction.UP);
                        }
                        break;
                    case KeyEvent.VK_DOWN:
                        if (snake.getDirection()!=Direction.UP){
                            snake.setDirection(Direction.DOWN);
                        }
                        break;
                    case KeyEvent.VK_LEFT:
                        if(snake.getDirection()!=Direction.RIGHT){
                            snake.setDirection(Direction.LEFT);
                        }
                        break;
                    case KeyEvent.VK_RIGHT:
                        if(snake.getDirection()!=Direction.LEFT){
                            snake.setDirection(Direction.RIGHT);
                        }
                        break;
                }
            }
        });
    }

    private void initTimer() {
        timer = new Timer();

        //初始化定时任务
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                snake.move();
                Node head = snake.getBody().getFirst();
                if(food.getX()==head.getX()&&food.getY()==head.getY()){
                    snake.eat(food);
                    food.random();
                }
                if(food1.getX()==head.getX()&&food1.getY()==head.getY()){
                    snake.eat(food1);
                    food1.random();
                }
                jPanel.repaint();
            }
        };
        //每100毫秒，执行一次定时任务
        timer.scheduleAtFixedRate(timerTask,0,100);
    }

    private void initSnake() {
        snake = new Snake();
    }

    //初始化游戏棋盘
    private void initGamePanel() {
        jPanel = new JPanel(){
           //绘制棋盘内容
            @Override
            public void paint(Graphics g) {
                //清空棋盘
                g.clearRect(0,0,600,600);
                //绘制40条横线
//                g.setColor(Color.BLACK);
                for (int i =0;i<=40;i++){
                    g.drawLine(0,i*15,600,i*15);
                }
                //绘制40条竖线
                for (int i =0;i<=40;i++){
                    g.drawLine(i*15,0,i*15,600);
                }
                //绘制食物
                g.fillRect(food.getX()*15, food.getY()*15,15,15);
                g.fillRect(food1.getX()*15, food1.getY()*15,15,15);
                //绘制蛇
//                g.setColor(Color.ORANGE);
                LinkedList<Node> body = snake.getBody();
                for (Node node : body) {
                    g.fillRect(node.getX()*15, node.getY()*15,15,15);
                }
            }
        };
        add(jPanel);
    }

    //初始化窗体参数
    private void initFrame() {
        //窗体的标题设置
        setTitle("贪吃蛇Vesion 1");
        //窗体的大小设置
        setSize(610,640);
        //窗体的可视化
        setVisible(true);
        //设置窗体初始化位置
        setLocation(400,140);
        //设置窗体不可改变大小
        setResizable(false);
        //设置关闭按钮的作用
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        //初始化食物
        initFood();
    }

    private void initFood() {
        food = new Node();
        food1 = new Node();
        food.random();
        food1.random();
    }
}
